import time
import unittest
from api.regloginapi import RegLoginApi
import requests
from common.utils import common_assert
from common.db_utils import clear_user_info
from parameterized import parameterized
from common.read_json import read_json
from time import sleep

class TestRegLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        # 调用封装清晰用户信息的方法
        clear_user_info()

    def setUp(self) -> None:
        # 创建session对象
        self.session = requests.session()
        # 封装类的实例化
        self.reg_login_api = RegLoginApi(self.session)

    def tearDown(self) -> None:
        self.session.close()

    # 获取图片验证码测试用例
    @parameterized.expand(read_json('/reglogin', 'img_verify_code'))
    def test01_img_verify_code(self, r, exp_status_code):
        # 1.调用封装的方法,获取响应结果
        # 实现接口请求
        response = self.reg_login_api.get_img_verify_code(r)
        # 2.断言
        self.assertEqual(exp_status_code, response.status_code)

    # 获取短信验证码测试用例
    @parameterized.expand(read_json('/reglogin', 'get_phone_verify_code'))
    def test02_phone_verify_code(self, phone, imgVerifyCode, type, exp_status_code, exp_status, description):
        # 1.调用封装的方法,获取响应结果
        # 实现获取图片验证码接口请求
        response = self.reg_login_api.get_img_verify_code(512345845554)
        # 实现获取短信验证码接口请求
        response = self.reg_login_api.get_phone_verify_code(phone, imgVerifyCode, type)
        json_data = response.json()
        # 2.断言
        common_assert(self, response, exp_status_code, exp_status, description)

    # 注册测试用例
    @parameterized.expand(read_json('/reglogin', 'user_register'))
    def test03_register(self, phone, password, verifycode, phone_code, dy_server, invite_phone, exp_status_code,
                        exp_status, description):
        # 1.调用封装的方法,获取响应结果
        # 实现获取图片验证码接口请求
        response = self.reg_login_api.get_img_verify_code(512345845554)
        # 实现获取短信验证码接口请求
        response = self.reg_login_api.get_phone_verify_code(phone, "8888", "reg")
        # 实现注册接口请求
        response = self.reg_login_api.user_register(phone, password, verifycode, phone_code, dy_server, invite_phone)
        response_dict = response.json()
        # 2.断言
        common_assert(self, response, exp_status_code, exp_status, description)

    # 登录测试用例
    @parameterized.expand(read_json('/reglogin', 'user_login'))
    def test04_user_login(self, keywords, password, exp_status_code, exp_status, description):
        # 1.调用封装的方法,获取响应结果
        # 实现登录接口请求
        response = self.reg_login_api.user_login(keywords, password)
        json_data = response.json()
        # 2.断言
        common_assert(self, response, exp_status_code, exp_status, description)
    @unittest.skip
    # 登录密码错误校验测试用例
    def test05_user_login_error(self):
        # 1.第一次密码错误
        response = self.reg_login_api.user_login("15878945622", "l123452")
        # 2.第二次密码错误
        response = self.reg_login_api.user_login("15878945622", "l123452")
        # 3.第三次密码错误
        response = self.reg_login_api.user_login("15878945622", "l123452")
        # 4.等待60S
        time.sleep(60)
        # 5.60s后登陆成功
        response = self.reg_login_api.user_login("15878945622", "l123456")
